<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <!-- import CSS -->
  <!-- import CSS (网络路径)-->
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
  <el-upload
          action="/upload"
          name="picFile"
          list-type="picture-card"
          :on-success="handleSuccess"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove">
    <i class="el-icon-plus"></i>
  </el-upload>
  <el-dialog :visible.sync="dialogVisible">
    <img width="100%" :src="dialogImageUrl" alt="">
  </el-dialog>
  <form>
    <input type="text" name="content" v-model="weibo.content" placeholder="说点什么">
    <input type="button" value="发表微博" @click="send()">
  </form>
</div>
</body>
<script src="js/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
  let v = new Vue({
    el: '#app',
    data() {
      return {
        dialogImageUrl: '',
        dialogVisible: false,
        weibo:{
          content:"",
          urls:""
        }
      };
    },
    methods: {
      handleRemove(file, fileList) {
        console.log(file, fileList);
        axios.get("/remove?name=" + file.response).then(function (response) {
          alert("删除完毕");
        })
        let arr = [];
        for (let file of fileList) {
          arr.push(file.response);
        }
        //把数组中多个图片路径 合并一个字符串赋值给urls
        v.weibo.urls = arr.toString();
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      handleSuccess(response, file, fileList) {
        //遍历fileList数组取出数组file对象图片的路径装进一个新的数组里面
        let arr = [];
        for (let file of fileList) {
          arr.push(file.response);
        }
        //把数组中多个图片路径 合并一个字符串赋值给urls
        v.weibo.urls = arr.toString();
      },
      send() {
        //发请求 发布微博
        axios.post("/insert", v.weibo).then(function (response) {
          if (response.data == 1) {
            alert("发布微博成功!");
            location.href = "/";//回到首页
          } else {
            alert("请先登录!");
            location.href = "/login.html";
          }
        })
      }
    }
  })
</script>
</html>